package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.domain.Result;
import com.he.domain.dto.CheckDTO;
import com.he.domain.dto.CommentDTO;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.vo.CommentBackVO;
import com.he.domain.vo.RecentCommentVO;
import com.he.enums.LikeTypeEnum;
import com.he.service.CommentService;
import com.he.strategy.context.LikeStrategyContext;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/24 23:59
 * @Desc 评论controller
 */
@Tag(name = "评论模块")
@RestController
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private LikeStrategyContext likeStrategyContext;


//    @PreAuthorize("permitAll()")
    @Operation(description = "查看评论列表")
    @GetMapping("/comment/list")
    public Result<?> listCommentVO(ConditionDTO conditionDTO){
        return Result.success(commentService.listCommentVO(conditionDTO));
    }


    @Operation(description = "点赞评论")
    @AccessLimit(seconds = 60, maxCount = 3)
    @PostMapping("/comment/{commentId}/like")
    public Result<?> likeComment(@PathVariable("commentId") Integer commentId) {
        likeStrategyContext.executeLikeStrategy(LikeTypeEnum.COMMENT, commentId);
        return Result.success();
    }


    @Operation(description = "添加评论")
    @PostMapping("/comment/add")
    public Result<?> addComment(@Validated @RequestBody CommentDTO commentDTO) {
        commentService.addComment(commentDTO);
        return Result.success();
    }

    @Operation(description = "查看后台评论列表")
    @GetMapping("/admin/comment/list")
    @PreAuthorize("@ps.hasPermission('news:comment:list')")
    public Result<PageResult<CommentBackVO>> listCommentBackVO(ConditionDTO condition) {
        return Result.success(commentService.listCommentBackVO(condition));
    }

    /**
     * 删除评论
     *
     * @param commentIdList 评论id
     * @return {@link Result<>}
     */
    @Operation(description = "删除评论")
    @PreAuthorize("@ps.hasPermission('news:comment:delete')")
    @DeleteMapping("/admin/comment/delete")
    public Result<?> deleteComment(@RequestBody List<Integer> commentIdList) {
        commentService.removeByIds(commentIdList);
        return Result.success();
    }

    /**
     * 查看文章最新评论
     *
     * @return {@link List<RecentCommentVO>}
     */
    @Operation(description = "查看最新评论")
    @GetMapping("/recent/{articleId}/comment")
    public Result<List<RecentCommentVO>> listArticleRecentCommentVO(@PathVariable("articleId") Integer typeId) {
        return Result.success(commentService.listRecentArticleCommentVO(typeId));
    }

    /**
     * 审核评论
     *
     * @param check 审核信息
     * @return {@link Result<>}
     */
    @Operation(description = "审核评论")
    @PreAuthorize("@ps.hasPermission('news:comment:pass')")
    @PutMapping("/admin/comment/pass")
    public Result<?> updateCommentCheck(@Validated @RequestBody CheckDTO check) {
        commentService.updateCommentCheck(check);
        return Result.success();
    }

    /**
     * 查看最新评论
     *
     * @return {@link List<RecentCommentVO>}
     */
    @Operation(description = "查看最新评论")
    @GetMapping("/recent/comment")
    public Result<List<RecentCommentVO>> listRecentCommentVO() {
        return Result.success(commentService.listRecentCommentVO());
    }
}
